package com.jinzhi.jzapi.utils;

import com.jinzhi.jzweb.utils.ValuesUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;

import static com.jinzhi.jzapi.utils.SpellHelper.getUpEname;

public class CerSynthesisUtil {

    public static void main(String[] args) throws IOException {
        String stuName = "张祥龙";
        String stuImg = "D:/2.jpg";
        String idCard = "411424198506241642";
        String cerNumber = "JZ20412008010091";
        String electronicSeal = "D:/upload/金职公章.png";

//        String backgroundPath = "D:/upload/证书模板参考（正面）.jpg";
//        String issueDate = "2020年6月15日";
//        String examinYear = "2020";
//        String examinMath = "12";
//        String cerLevelC = "中级";
//        String orgPrincipalImg = "";
//        String assPointPrincipalImg = "";
        // 背景图路径，考生姓名，考生头像图路径，身份证号码，证书编号，发证时间，考试年份，考试月份，证书等级中文，发证机构公章图路径，机构负责人签章图路径，考核点签章图路径
        // [backgroundPath, stuName, stuImg, idCard, cerNumber, issueDate, examinYear, examinMath, cerLevelC, electronicSeal, orgPrincipalImg, assPointPrincipalImg]
//        overlapImage(backgroundPath, stuName, stuImg, idCard, cerNumber, issueDate, examinYear, examinMath, cerLevelC,
//                electronicSeal, orgPrincipalImg, assPointPrincipalImg);

        String backgroundPath1 = "D:/upload/督导员证.jpg";

        String numPeriods = "01";
        String trainingNumber = "48";
        String traYear = "2020";
        String traMath = "08";
        String traDay = "18";
        String cerYear = "2020";
        String cerMath = "09";
        String cerDay = "23";
        String workUnit="湖南金职伟业现代家庭产业有限公司";
        String effectiveDate = "2020-08-14至2023-08-14";
        // 合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时）， 培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章
        // [type,backgroundPath,stuName,stuImg,idCard,cerNumber,numPeriods,trainingNumber,traYear,traMath,traDay,cerYear,cerMath,cerDay,electronicSeal]
        //考评员
        //overTraExaImageRecovery(backgroundPath1, stuName, stuImg, idCard, cerNumber, effectiveDate,workUnit,1);
        //师资
        //overTrainingImageRecovery(0,backgroundPath1, stuName, stuImg, idCard, cerNumber,numPeriods,trainingNumber,traYear,traMath,traDay,cerYear,cerMath,cerDay,electronicSeal,workUnit,1);
        //督导员-结业证书
        //overTraSupervisorGradiningImage(0,backgroundPath1, stuName, stuImg, idCard, cerNumber,numPeriods,trainingNumber,traYear,traMath,traDay,cerYear,cerMath,cerDay,electronicSeal,workUnit);
        //考务技术员-结业证书
        //overTraAraGradiningImage(0,backgroundPath1, stuName, stuImg, idCard, cerNumber,numPeriods,trainingNumber,traYear,traMath,traDay,cerYear,cerMath,cerDay,electronicSeal,workUnit);
        //考务技术员证
        //overTraExaAffImage(backgroundPath1, stuName, stuImg, idCard, cerNumber, effectiveDate,workUnit);
        //督导员证
        overTraSupervisorExaAffImage(backgroundPath1, stuName, stuImg, idCard, cerNumber, effectiveDate,workUnit);
        //String backgroundPath2 = "D:/upload/考评员证-正面.jpg";
        //String effectiveDate = "2020-08-14至2023-08-14";
        // 背景图，学员姓名，学员图像， 身份证号码，证书编码，证书名称，有效日期
        // [backgroundPath, stuName, stuImg, idCard, cerNumber, cerName, effectiveDate]
//        overTraExaImage(backgroundPath2, stuName, stuImg, idCard, cerNumber, effectiveDate);
    }

    /**
    * 考务技术员证合成方法-幼儿照护-产后恢复
    *
    * @Description:  考评员培训照片合成方法 ,参数说明：背景图，学员姓名，学员图像， 身份证号码，证书编码，证书名称，有效日期，工作单位
    * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, cerName, effectiveDate]
    * @return: java.lang.String
    * @Author: kl
    * @Date: 2020-07-20
    */
    public static String overTraExaAffImage(String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String effectiveDate,String workUnit) throws IOException {
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(705, 983, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(216, 258, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("微软雅黑", Font.BOLD, 32));
        //考生姓名
        g.drawString(stuName, 308, 672);

        g.setFont(new Font("黑体", 2, 40));
        //考生头像
        g.drawImage(stuImgs, 235, 369, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //身份证
        g.drawString(idCard, 272, 718);
        //证书编号
        g.drawString(cerNumber, 272, 751);

        //有效日期
        g.drawString(effectiveDate, 272, 784);
        if(workUnit.length()>16) {
            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 20));
            //工作单位
            g.drawString(workUnit.substring(0,16), 272, 810);
            g.drawString(workUnit.substring(16,workUnit.length()), 272, 830);
        }else{
            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 21));
            //工作单位
            g.drawString(workUnit, 272, 813);
        }
        //工作单位
        //g.drawString(workUnit, 272, 815);

        //完成模板修改
        g.dispose();

        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";


        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }

    
    /**
    * 督导员证合成方法-幼儿照护-产后恢复
    *
    * @Description:  督导员证合成方法 ,参数说明：背景图，学员姓名，学员图像， 身份证号码，证书编码，证书名称，有效日期，工作单位
    * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, cerName, effectiveDate]
    * @return: java.lang.String
    * @Author: kl
    * @Date: 2020-07-20
    */
    public static String overTraSupervisorExaAffImage(String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String effectiveDate,String workUnit) throws IOException {
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(705, 983, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(168, 224, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(255, 255, 255);
        g.setColor(color);
        g.setFont(new Font("微软雅黑", Font.BOLD, 32));
        //考生姓名
        g.drawString(stuName, 309, 663);

        g.setFont(new Font("黑体", 2, 40));
        //考生头像
        g.drawImage(stuImgs, 270, 395, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 23));
        //身份证
        g.drawString(idCard, 272, 721);
        //证书编号
        g.drawString(cerNumber, 272, 754);

        //有效日期
        g.drawString(effectiveDate, 272, 787);

        if(workUnit.length()>16) {
            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 20));
            //工作单位
            g.drawString(workUnit.substring(0,16), 272, 813);
            g.drawString(workUnit.substring(16,workUnit.length()), 272, 833);
        }else{
            //工作单位
            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 21));
            g.drawString(workUnit, 272, 816);
        }
        //工作单位
        //g.drawString(workUnit, 272, 818);

        //完成模板修改
        g.dispose();

        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";


        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }

    
    
    /**
     * 考试证书照片合成方法
     *
     * @Description: 考试证书照片合成方法，参数：合成类型（0带公章，1不带公章），背景图路径，考生姓名，考生头像图路径，身份证号码，证书编号，发证时间，考试年份，考试月份，证书等级中文，
     * 发证机构公章图路径，机构负责人签章图路径，考核点签章图路径
     * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, issueDate, examinYear, examinMath, cerLevelC, electronicSeal, orgPrincipalImg, assPointPrincipalImg]
     * @return: void
     * @Author: kl
     * @Date: 2020-06-16
     */
    public static String overlapImage(int type,String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String issueDate, String examinYear, String examinMath,
                                      String cerLevelC, String electronicSeal, String orgPrincipalImg, String assPointPrincipalImg) throws IOException {
        // 设置背景图片大小，暂时按照分辨率是96像素/英寸时，A4纸的尺寸的图像的像素是794×1123；(windows默认)
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(120, 160, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(51, 51, 51);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 24));
        if (counthanzi(stuName) == 2) {
            //考生姓名
            g.drawString(stuName, 890, 420);
            //考生姓名拼音
            g.drawString(getUpEname(stuName), 863, 460);
        } else if (counthanzi(stuName) == 3) {
            //考生姓名
            g.drawString(stuName, 880, 420);
            //考生姓名拼音
            g.drawString(getUpEname(stuName), 845, 460);
        } else {
            //考生姓名
            g.drawString(stuName, 866, 420);
            //考生姓名拼音
            g.drawString(getUpEname(stuName), 838, 460);
        }

        //考生头像
        g.drawImage(stuImgs, 858, 220, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 20));
        //身份证
        g.drawString(idCard, 295, 463);
        //证书编号
        g.drawString(cerNumber, 295, 513);
        //发证日期
        g.drawString(issueDate, 295, 622);

        if(type == 0) {
            //电子公章，以水印的形式插入
            //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(electronicSeal);
            //得到Image对象。
            Image img = imgIcon.getImage();
            float alpha = 1.0f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 表示水印图片的位置
            g.drawImage(img, 320, 515, 150, 150, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        }


        //考试年份（中文部分）
        g.drawString(examinYear, 213, 240);
        //考试月份（中文部分）
        g.drawString(examinMath, 295, 240);
        //认证批次对应的证书等级（中文部分）
        g.drawString(cerLevelC, 445, 273);

        g.setFont(new Font("Arial", Font.ITALIC + Font.BOLD, 14));
        //初级：Primary    中级：Intermediate    高级：Advanced
        if (cerLevelC.equals("初级")) {
            //认证批次对应的证书等级（英文部分）
            g.drawString("Primary", 329, 370);
        } else if (cerLevelC.equals("中级")) {
            //认证批次对应的证书等级（英文部分）
            g.drawString("Intermediate", 315, 370);
        } else {
            //认证批次对应的证书等级（英文部分）
            g.drawString("Advanced", 323, 370);
        }

        if (examinMath.equals("1")) {
            g.drawString("January" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("2")) {
            g.drawString("February" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("3")) {
            g.drawString("March" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("4")) {
            g.drawString("April" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("5")) {
            g.drawString("May" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("6")) {
            g.drawString("June" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("7")) {
            g.drawString("July" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("8")) {
            g.drawString("August" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("9")) {
            g.drawString("September" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("10")) {
            g.drawString("October" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else if (examinMath.equals("11")) {
            g.drawString("November" + " " + examinYear, 725, 338);//考试年月（英文部分）
        } else {
            g.drawString("December" + " " + examinYear, 725, 338);//考试年月（英文部分）
        }

        //完成模板修改
        g.dispose();

        String outPutPath;
        if(type == 0){
            outPutPath = ValuesUtil.localUrl + "/upload/cerSynthesis/" + stuName + cerNumber + ".png";
        }else{
            outPutPath = ValuesUtil.localUrl + "/upload/cerSynthesisNoSale/" + stuName + cerNumber + ".png";
        }
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }

    /**
     * 重新设置图片大小
     *
     * @return      
     * @param width
     * @param height
     * @param bufferedImage
     */
    private static BufferedImage resizeImage(int width, int height, BufferedImage bufferedImage) {
        BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        newBufferedImage.getGraphics().drawImage(bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        return newBufferedImage;
    }

    //统计汉字个数的方法
    public static int counthanzi(String text) {
        int amount = 0;
        for (int i = 0; i < text.length(); i++) {
            boolean matches = Pattern.matches("^[\u4E00-\u9Fa5]{0,}$", text.charAt(i) + "");
            if (matches) {
                amount++;
            }
        }
        return amount;
    }


    /**
     * 师资证合成方法-幼儿照护
     *
     * @Description: 师资证合成方法，参数说明：合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时），
     * 培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章，工作单位，级别
     * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, trainingName, trainingNumber, trainingYear, trainingMath, trainingDay, cerName, electronicSeal]
     * @return: java.lang.String
     * @Author: kl
     * @Date: 2020-07-17
     */
    public static String overTrainingImage(int type,String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String numPeriods, String trainingNumber,
                                           String traYear, String traMath, String traDay,String cerYear, String cerMath, String cerDay, String electronicSeal,String workUnit,int grade ) throws IOException {
        // 设置背景图片大小，暂时按照分辨率是96像素/英寸时，A4纸的尺寸的图像的像素是794×1123；(windows默认)
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(144, 192, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(51, 51, 51);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 18));
        int a = 690-counthanzi(stuName)*18;
        g.drawString(stuName, a, 230);
//        if (counthanzi(stuName) == 2) {
//            //考生姓名
//            g.drawString(stuName, 682, 230);
//        } else if (counthanzi(stuName) == 3) {
//            //考生姓名
//            g.drawString(stuName, 672, 230);
//        } else {
//            //考生姓名
//            g.drawString(stuName, 662, 230);
//        }

        //证书文字-培训年份
        g.drawString(traYear, 785, 232);
        //证书文字-培训月份
        g.drawString(traMath, 862, 232);
        //证书文字-培训日期
        g.drawString(traDay, 919, 232);
        //证书文字-培训结束月份
        g.drawString(cerMath, 618, 269);
        //证书文字-培训结束日期
        g.drawString(cerDay, 676, 269);
        if(grade==0) {
            //考核期数
            g.drawString(numPeriods, 902, 303);
            //培训章节数（学时）
            g.drawString(trainingNumber, 674, 340);
        }else{
            //考核期数
            g.drawString(numPeriods, 934, 303);
            //培训章节数（学时）
            g.drawString(trainingNumber, 685, 340);
        }

        //考生头像
        g.drawImage(stuImgs, 242, 342, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 18));
        //身份证
        g.drawString(idCard, 230, 600);
        //证书编号
        g.drawString(cerNumber, 230, 637);
        //工作单位
        g.drawString(workUnit, 230, 676);

        if(type == 0){
            //电子公章，以水印的形式插入
            //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(electronicSeal);
            //得到Image对象。
            Image img = imgIcon.getImage();
            float alpha = 1.0f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 表示水印图片的位置
            g.drawImage(img, 720, 540, 150, 150, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        }
        g.setFont(new Font("黑体", Font.BOLD, 19));
        //证书落笔-培训年份
        g.drawString(cerYear, 715, 674);
        //证书落笔-培训月份
        g.drawString(cerMath, 790, 673);
        //证书落笔-培训日期
        g.drawString(cerDay, 838, 672);

        //完成模板修改
        g.dispose();

        String outPutPath;
        if(type == 0){
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";
        }else{
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesisNoSale/" + stuName + cerNumber + ".png";
        }

        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }

    /**
     * 师资证合成方法-产后恢复
     *
     * @Description: 师资证合成方法，参数说明：合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时），
     * 培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章，工作单位，级别
     * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, trainingName, trainingNumber, trainingYear, trainingMath, trainingDay, cerName, electronicSeal]
     * @return: java.lang.String
     * @Author: kl
     * @Date: 2020-07-17
     */
    public static String overTrainingImageRecovery(int type,String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String numPeriods, String trainingNumber,
                                           String traYear, String traMath, String traDay,String cerYear, String cerMath, String cerDay, String electronicSeal,String workUnit,int grade ) throws IOException {
        // 设置背景图片大小，暂时按照分辨率是96像素/英寸时，A4纸的尺寸的图像的像素是794×1123；(windows默认)
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(144, 192, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(51, 51, 51);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 18));
        int a = 690-counthanzi(stuName)*18;
        g.drawString(stuName, a, 230);
//        if (counthanzi(stuName) == 2) {
//            //考生姓名
//            g.drawString(stuName, 682, 230);
//        } else if (counthanzi(stuName) == 3) {
//            //考生姓名
//            g.drawString(stuName, 672, 230);
//        } else {
//            //考生姓名
//            g.drawString(stuName, 662, 230);
//        }

        //证书文字-培训年份
        g.drawString(traYear, 785, 232);
        //证书文字-培训月份
        g.drawString(traMath, 862, 232);
        //证书文字-培训日期
        g.drawString(traDay, 919, 232);
        //证书文字-培训结束月份
        g.drawString(cerMath, 618, 269);
        //证书文字-培训结束日期
        g.drawString(cerDay, 676, 269);
        if(grade==0) {
            //考核期数
            g.drawString(numPeriods, 902, 303);
            //培训章节数（学时）
            g.drawString(trainingNumber, 674, 340);
        }else{
            //考核期数
            g.drawString(numPeriods, 934, 303);
            //培训章节数（学时）
            g.drawString(trainingNumber, 685, 340);
        }

        //考生头像
        g.drawImage(stuImgs, 242, 342, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 18));
        //身份证
        g.drawString(idCard, 230, 600);
        //证书编号
        g.drawString(cerNumber, 230, 637);
        //工作单位
        g.drawString(workUnit, 230, 676);

        if(type == 0){
            //电子公章，以水印的形式插入
            //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(electronicSeal);
            //得到Image对象。
            Image img = imgIcon.getImage();
            float alpha = 1.0f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 表示水印图片的位置
            g.drawImage(img, 720, 540, 150, 150, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        }
        g.setFont(new Font("黑体", Font.BOLD, 19));
        //证书落笔-培训年份
        g.drawString(cerYear, 715, 674);
        //证书落笔-培训月份
        g.drawString(cerMath, 790, 673);
        //证书落笔-培训日期
        g.drawString(cerDay, 838, 672);

        //完成模板修改
        g.dispose();

        String outPutPath;
        if(type == 0){
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";
        }else{
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesisNoSale/" + stuName + cerNumber + ".png";
        }

        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }


    /**
     * 考务技术员结业证合成方法-幼儿照护-产后恢复
     *
     * @Description: 考务技术员结业证合成方法，参数说明：合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时），
     * 培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章，工作单位
     * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, trainingName, trainingNumber, trainingYear, trainingMath, trainingDay, cerName, electronicSeal]
     * @return: java.lang.String
     * @Author: kl
     * @Date: 2020-07-17
     */
    public static String overTraAraGradiningImage(int type,String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String numPeriods, String trainingNumber,
                                           String traYear, String traMath, String traDay,String cerYear, String cerMath, String cerDay, String electronicSeal,String workUnit) throws IOException {
        // 设置背景图片大小，暂时按照分辨率是96像素/英寸时，A4纸的尺寸的图像的像素是794×1123；(windows默认)
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(120, 160, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(51, 51, 51);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 18));
        //考生姓名
        int a = 714-counthanzi(stuName)*18;
        g.drawString(stuName, a, 231);
//        if (counthanzi(stuName) == 2) {
//            //考生姓名
//            
//        } else if (counthanzi(stuName) == 3) {
//            //考生姓名
//            g.drawString(stuName, 684, 231);
//        } else {
//            //考生姓名
//            g.drawString(stuName, 674, 231);
//        }

        //证书文字-培训年份
        g.drawString(traYear, 793, 231);
        //证书文字-培训月份
        g.drawString(traMath, 866, 231);
        //证书文字-培训日期
        g.drawString(traDay, 918, 231);
        //考核期数
        g.drawString(numPeriods, 893, 303);
        //培训章节数（学时）
        g.drawString(trainingNumber, 715, 340);

        //考生头像
        g.drawImage(stuImgs, 254, 374, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 20));
        //身份证
        g.drawString(idCard, 270, 613);
        //证书编号
        g.drawString(cerNumber, 270, 648);
        //工作单位
        g.drawString(workUnit, 270, 683);

        if(type == 0){
            //电子公章，以水印的形式插入
            //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(electronicSeal);
            //得到Image对象。
            Image img = imgIcon.getImage();
            float alpha = 1.0f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 表示水印图片的位置
            g.drawImage(img, 720, 540, 150, 150, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        }

        //证书落笔-培训年份
        g.drawString(cerYear, 708, 671);
        //证书落笔-培训月份
        g.drawString(cerMath, 782, 671);
        //证书落笔-培训日期
        g.drawString(cerDay, 831, 671);

        //完成模板修改
        g.dispose();

        String outPutPath;
        if(type == 0){
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";
        }else{
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesisNoSale/" + stuName + cerNumber + ".png";
        }

        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }



    /**
     * 督导员结业证合成方法-幼儿照护
     *
     * @Description: 督导员结业证合成方法，参数说明：合成类型（0带公章，1不带公章），背景图，学员姓名，学员图像， 身份证号码，证书编码，考核期数，培训章节数（学时），
     * 培训年份，培训月份，培训日期，获证年份，获证年份，获证年份，电子公章，工作单位
     * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, trainingName, trainingNumber, trainingYear, trainingMath, trainingDay, cerName, electronicSeal]
     * @return: java.lang.String
     * @Author: kl
     * @Date: 2020-07-17
     */
    public static String overTraSupervisorGradiningImage(int type,String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String numPeriods, String trainingNumber,
                                           String traYear, String traMath, String traDay,String cerYear, String cerMath, String cerDay, String electronicSeal,String workUnit) throws IOException {
        // 设置背景图片大小，暂时按照分辨率是96像素/英寸时，A4纸的尺寸的图像的像素是794×1123；(windows默认)
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        System.out.println(backgroundPath);
        BufferedImage backgroundImage = resizeImage(1123, 794, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(120, 160, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(51, 51, 51);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 18));
        int a = 714-counthanzi(stuName)*18;
        g.drawString(stuName, a, 231);
//        if (counthanzi(stuName) == 2) {
//            //考生姓名
//            g.drawString(stuName, 694, 231);
//        } else if (counthanzi(stuName) == 3) {
//            //考生姓名
//            g.drawString(stuName, 684, 231);
//        } else {
//            //考生姓名
//            g.drawString(stuName, 674, 231);
//        }

        //证书文字-培训年份
        g.drawString(traYear, 797, 231);
        //证书文字-培训月份
        g.drawString(traMath, 870, 231);
        //证书文字-培训日期
        g.drawString(traDay, 918, 231);
        //考核期数
        g.drawString(numPeriods, 862, 303);
        //培训章节数（学时）
        g.drawString(trainingNumber, 710, 340);

        //考生头像
        g.drawImage(stuImgs, 254, 374, stuImgs.getWidth(), stuImgs.getHeight(), null);

        g.setFont(new Font("黑体", Font.BOLD, 19));

        //身份证
        g.drawString(idCard, 270, 613);
        //证书编号
        g.drawString(cerNumber, 270, 648);
        //工作单位
        g.drawString(workUnit, 270, 683);

        if(type == 0){
            //电子公章，以水印的形式插入
            //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(electronicSeal);
            //得到Image对象。
            Image img = imgIcon.getImage();
            float alpha = 1.0f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
            // 表示水印图片的位置
            g.drawImage(img, 720, 540, 150, 150, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        }

        //证书落笔-培训年份
        g.drawString(cerYear, 708, 671);
        //证书落笔-培训月份
        g.drawString(cerMath, 782, 671);
        //证书落笔-培训日期
        g.drawString(cerDay, 831, 671);

        //完成模板修改
        g.dispose();

        String outPutPath;
        if(type == 0){
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";
        }else{
            outPutPath = ValuesUtil.localUrl + "/upload/traSynthesisNoSale/" + stuName + cerNumber + ".png";
        }

        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }

    
    /**
    * 考评员证合成方法-幼儿照护
    *
    * @Description:  考评员培训照片合成方法 ,参数说明：背景图，学员姓名，学员图像， 身份证号码，证书编码，证书名称，有效日期，工作单位，级别
    * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, cerName, effectiveDate]
    * @return: java.lang.String
    * @Author: kl
    * @Date: 2020-07-20
     *
     *
     *
    */
    public static String overTraExaImage(String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String effectiveDate,String workUnit,int grade) throws IOException {
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(705, 983, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(216, 258, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 31));



        if(grade==0) {
            //考生姓名
            g.drawString(stuName, 322, 686);

            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 32));
            //考生头像
            g.drawImage(stuImgs, 238, 387, stuImgs.getWidth(), stuImgs.getHeight(), null);

            g.setFont(new Font("黑体", Font.BOLD, 23));
            //身份证
            g.drawString(idCard, 262, 730);
            //证书编号
            g.drawString(cerNumber, 262, 763);
            //有效日期
            g.drawString(effectiveDate, 262, 796);
            if(workUnit.length()>16) {
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 20));
                //工作单位
                g.drawString(workUnit.substring(0,16), 262, 822);
                g.drawString(workUnit.substring(16,workUnit.length()), 262, 842);
            }else{
                //工作单位
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 21));
                g.drawString(workUnit, 262, 825);
            }

        }else{
            //考生姓名
            g.drawString(stuName, 322, 682);

            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 32));
            //考生头像
            g.drawImage(stuImgs, 239, 385, stuImgs.getWidth(), stuImgs.getHeight(), null);

            g.setFont(new Font("黑体", Font.BOLD, 23));
            //身份证
            g.drawString(idCard, 262, 727);
            //证书编号
            g.drawString(cerNumber, 262, 760);
            //有效日期
            g.drawString(effectiveDate, 262, 793);
            if(workUnit.length()>16) {
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 20));
                //工作单位
                g.drawString(workUnit.substring(0,16), 262, 819);
                g.drawString(workUnit.substring(16,workUnit.length()), 262, 839);
            }else{
                //工作单位
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 21));
                g.drawString(workUnit, 262, 822);
            }

        }
        //完成模板修改
        g.dispose();

        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";
        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }

    /**
     * 考评员证合成方法-产后恢复
     *
     * @Description:  考评员培训照片合成方法 ,参数说明：背景图，学员姓名，学员图像， 身份证号码，证书编码，证书名称，有效日期，工作单位，级别
     * @Param: [backgroundPath, stuName, stuImg, idCard, cerNumber, cerName, effectiveDate]
     * @return: java.lang.String
     * @Author: kl
     * @Date: 2020-07-20
     *
     *
     *
     */
    public static String overTraExaImageRecovery(String backgroundPath, String stuName, String stuImg, String idCard, String cerNumber, String effectiveDate,String workUnit,int grade) throws IOException {
//        BufferedImage backgroundImage1 = ImageIO.read(new File(backgroundPath));//如果想获得原始图片，可直接用imageIo
        //将实际尺寸转换成英寸，再按照96像素/英寸比例计算得到大小
        BufferedImage backgroundImage = resizeImage(705, 983, ImageIO.read(new File(backgroundPath)));
        BufferedImage stuImgs = resizeImage(216, 258, ImageIO.read(new File(stuImg)));

        //在背景图片中添加入需要写入的信息，
        Graphics2D g = backgroundImage.createGraphics();

        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //设置为透明覆盖
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));

        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.setFont(new Font("黑体", Font.BOLD, 31));



        if(grade==0) {
            //考生姓名
            g.drawString(stuName, 322, 688);

            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 32));
            //考生头像
            g.drawImage(stuImgs, 239, 388, stuImgs.getWidth(), stuImgs.getHeight(), null);

            g.setFont(new Font("黑体", Font.BOLD, 23));
            //身份证
            g.drawString(idCard, 262, 732);
            //证书编号
            g.drawString(cerNumber, 262, 765);

            //有效日期
            g.drawString(effectiveDate, 262, 798);

            if(workUnit.length()>16) {
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 20));
                //工作单位
                g.drawString(workUnit.substring(0,16), 262, 824);
                g.drawString(workUnit.substring(16,workUnit.length()), 262, 844);
            }else{
                //工作单位
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 21));
                g.drawString(workUnit, 262, 827);
            }
            //工作单位
            //g.drawString(workUnit, 262, 829);
        }else{
            //考生姓名
            g.drawString(stuName, 322, 680);

            g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 32));
            //考生头像
            g.drawImage(stuImgs, 239, 382, stuImgs.getWidth(), stuImgs.getHeight(), null);

            g.setFont(new Font("黑体", Font.BOLD, 23));
            //身份证
            g.drawString(idCard, 262, 725);
            //证书编号
            g.drawString(cerNumber, 262, 758);

            //有效日期
            g.drawString(effectiveDate, 262, 791);

            if(workUnit.length()>16) {
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 20));
                //工作单位
                g.drawString(workUnit.substring(0,16), 262, 817);
                g.drawString(workUnit.substring(16,workUnit.length()), 262, 837);
            }else{
                //工作单位
                g.setFont(new Font("方正粗黑宋简体", Font.BOLD, 21));
                g.drawString(workUnit, 262, 820);
            }
            //工作单位
            //g.drawString(workUnit, 262, 822);
        }

        //完成模板修改
        g.dispose();

        String outPutPath = ValuesUtil.localUrl + "/upload/traSynthesis/" + stuName + cerNumber + ".png";


        // 输出新的图片
        ImageIO.write(backgroundImage, "png", new File(outPutPath));

        return outPutPath;
    }
}
